import { _decorator, Component, Label, Node, tween, Tween, Vec3 } from 'cc';
import { FishLoad } from './FishLoad';
import { CricleLoad } from './CricleLoad';
import { Circle } from './Circle';
import { Fish } from './Fish';
import { Data } from './Data';
import { CollectLoad } from './CollectLoad';
const { ccclass, property } = _decorator;

@ccclass('GameScene')
export class GameScene extends Component {

    //鱼层
    @property(Node)
    private FishLoad: Node = null;

    //网层
    @property(Node)
    private NetLoad: Node = null;

    //已经收集文字
    @property(Label)
    private AlreadyCollectLabel: Label = null;
    private CollectNum: number = 0;

    //发网文字
    @property(Label)
    private SendNetLabel: Label = null;
    private NetNum: number = 50;

    //提示文字
    @property(Label)
    private TipLabel: Label = null;

    //收藏层
    @property(Node)
    private CollectLoad: Node = null;

    //显示圈
    @property(Node)
    private ShowCircle: Node = null;

    //发网按钮
    @property(Node)
    private SendNetBtn: Node = null;

    protected onLoad(): void {
        this.Init();
    }


    /**
     * 初始化
     */
    public Init() {
        this.CollectNum = 0;
        this.AlreadyCollectLabel.string = "已收集:" + this.CollectNum;

        this.NetNum = 50;
        this.SendNetLabel.string = "发网:" + this.NetNum;

        this.FishLoad.getComponent(FishLoad).Init();
        this.NetLoad.getComponent(CricleLoad).Init();

        this.TipLabel.node.active = false;
        this.ShowCircle.active = false;
    }

    /**
     * 点击法网文字
     */
    public ClickSendNetLabel() {
        let circle_num: number = 0;
        for (let circle of this.NetLoad.children) {
            if (circle.getComponent(Circle).currentIndex == -1 && this.NetNum > 0) {
                this.NetNum--;
                this.SendNetLabel.string = "发网:" + this.NetNum;
                circle_num++

                let circle_index: number = Data.ReturnFishColor();
                circle.getComponent(Circle).UpdateCircle(circle_index);
                circle.getComponent(Circle).PlayAni(circle_num);
            }
        }
    }

    /**
     * 点击彩虹概率
     */
    private ClickRainbow() {
        Data.FishRainbowRate += 0.1;
        console.log("彩虹概率", (100 * Data.FishRainbowRate) + "%")
    }

    /**
     * 点击渔网+10
     */
    private ClickAddNet() {
        this.PlayNetAnimation(10)

    }

    /**
     * 播放渔网动画
     */
    public PlayNetAnimation(_net: number) {
        this.NetNum += _net;
        this.SendNetLabel.string = "发网:" + this.NetNum;

        this.TipLabel.string = "渔网+" + _net;
        this.TipLabel.node.active = true;

        Tween.stopAllByTarget(this.TipLabel.node)
        this.TipLabel.node.setPosition(new Vec3(0, 55))

        tween(this.TipLabel.node).to(0.5, { position: new Vec3(0, 115) },).delay(0.5).call(() => {
            this.TipLabel.node.active = false;
        }).start();


    }

    /**
     * 抓鱼
     */
    public CatchFish(_index: number) {
        let ishave: boolean = false;
        for (let fish of this.FishLoad.children) {
            if (fish.getComponent(Fish).NowIndex == _index) {

                for (let i = 0; i < this.NetLoad.getComponent(CricleLoad).ChooseCircle.length; i++) {
                    this.NetLoad.getComponent(CricleLoad).ChooseCircle[i].getComponent(Circle).PlayAni1(i, fish.getWorldPosition());
                }

                this.scheduleOnce(() => {
                    this.CollectNum++;
                    this.AlreadyCollectLabel.string = "已收集:" + this.CollectNum;
                    fish.getComponent(Fish).UpdateFish(Data.ReturnFishColor());
                    fish.getComponent(Fish).PlayShowAni(1);
                    this.PlayNetAnimation(_index == Data.FishRainbowIndex ? 10 : 1);
                    this.PlaySendNetAnimation(_index, fish.getWorldPosition());
                    this.NetLoad.getComponent(CricleLoad).ChooseReset(true);
                    // this.ClickSendNetLabel();

                    this.CollectLoad.getComponent(CollectLoad).AddFish(_index, fish.getWorldPosition());
                }, 0.7)

                ishave = true;
                break;
            }
        }

        if (!ishave) {
            this.NetLoad.getComponent(CricleLoad).ChooseReset(false);
            // this.ClickSendNetLabel();
        }
    }

    /**
     * 播放发网动画
     */
    public PlaySendNetAnimation(_index: number, _pos: Vec3) {

        Tween.stopAllByTarget(this.ShowCircle);
        this.ShowCircle.setWorldPosition(_pos);
        this.ShowCircle.active = true;
        this.ShowCircle.getComponent(Circle).UpdateCircle(_index);

        tween(this.ShowCircle).to(0.3, { worldPosition: this.SendNetBtn.getWorldPosition() },).call(() => {
            this.ShowCircle.active = false;
            tween(this.SendNetBtn).set({ scale: new Vec3(1.2, 1.2, 1.2) }).to(0.1, { scale: new Vec3(1, 1, 1) },).start();
        }).start();

    }

}


